﻿namespace WebPacker.Tool.Tests.Sources
{
	using System.Linq;
	using NUnit.Framework;
	using WebPacker.Configuration;
	using WebPacker.Packs;
	using WebPacker.Tool.Sources;
	using WebPacker.Tool.Tests.EqualityComparers;

	[TestFixture]
	public class FilesSourceTest
	{
		[Test]
		public void OpenFilesDifferentTypesTest()
		{
			var files = new string[] { "samples/test1.js", "samples/test2.js", "samples/style1.css" };
			var source = new FilesSource(Configuration.GetDefaults(), files);
			Assert.Throws<ToolException>(() => source.Open());
		}

		[Test, Sequential]
		public void OpenFilesTest(
			[Values(PackType.JavaScript, PackType.Css)]
			PackType packType,
			[Values(
				new string[] { "samples/test1.js", "samples/test2.js" }, 
				new string[] { "samples/style1.css" })]
			string[] files,
			[Values("testPack", null)]
			string packName)
		{
			var configuration = Configuration.GetDefaults();

			var source = packName != null ? new FilesSource(configuration, packName, files) : new FilesSource(configuration, files);
			source.Open();

			ConfigurationTest.AssertConfiguraionEquals(source.Configuration, Configuration.GetDefaults());

			var actual = source.Packs;
			Assert.IsNotNull(actual);

			var expected = new PackInfo[]
			{
				new PackInfo
				{
					Name = packName ?? "pack",
					PackType = packType,
					Resources = files.Select(x => new ResourceInfo { Name = x }).ToArray()
				}
			};

			Assert.That(actual, Is.EquivalentTo(expected).Using(new PackInfoEqualityComparer()));
		}
	}
}
